<template>
  <div>
    <h2>测试题</h2>
    <div v-for="(item, index) in arr" :key="index">
      <span>{{ item.num1 }}</span>
      <span>+</span>
      <span>{{ item.num2 }}</span>
      <span>=</span>
      <input type="number" v-model="item.result" />
      <button @click.prevent="sum(index)" :disabled="item.submitted">提交</button>
    </div>
    <br />
    <div>
      <span
        v-for="(item, ind) in arr"
        :key="ind"
        :class="{
          right: item.status === '正确',
          error: item.status === '错误',
          undo: item.status === '未完成',
        }"
        >{{ ind + 1 }}: {{ item.status }}</span
      >
    </div>
  </div>
</template>

<script>
export default {
  //随机数组数据
  data() {
    return {
      arr: [
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          result: '',
          submitted: false,
          status: '未完成',
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          result: '',
          submitted: false,
          status: '未完成',
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          result: '',
          submitted: false,
          status: '未完成',
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          result: '',
          submitted: false,
          status: '未完成',
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          result: '',
          submitted: false,
          status: '未完成',
        },
      ],
    }
  },
  methods: {
    sum(index) {
      if (this.arr[index].result == '') {
        alert('输入值')
        return
      }
      if (
        this.arr[index].result ==
        this.arr[index].num1 + this.arr[index].num2
      ) {
        this.arr[index].status = '正确'
        this.arr[index].submitted = true
      } else {
        this.arr[index].status = '错误'
      }
    },
  },
}
</script>

<style>
.right {
  color: green;
}

.error {
  color: red;
}

.undo {
  color: #ccc;
}
</style>
